package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shipping.model.po.ChuanYuanKuDongTaiSavePO;
import com.ct.application.shipping.model.po.ChuanYuanKuDongTaiSearchPO;
import com.ct.application.shipping.model.vo.ChuanYuanKuDongTaiVO;
import com.ct.application.shipping.service.ChuanYuanKuDongTaiService;
import com.ct.application.sysuser.model.vo.SysUserDetailVO;
import com.ct.application.sysuser.service.ISystemUserService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.ChuanYuanKuDongTaiMapper;
import com.ct.shipping.model.ChuanYuanKuDongTaiMapperSearchPO;
import com.ct.shipping.model.ChuanYuanKuDongTaiMapperVO;
import com.ct.shipping.orm.ChuanYuanKuDO;
import com.ct.shipping.orm.ChuanYuanKuDongTaiDO;
import com.ct.shipping.orm.ChuanYuanKuDongTaiDO;
import com.ct.utils.date.DateUtils;
import com.ct.utils.threadlocal.UserThreadLocal;
import com.ct.utils.valid.ValidatorUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 船员库动态记录 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class ChuanYuanKuDongTaiServiceImpl extends BaseServiceImpl<ChuanYuanKuDongTaiMapper, ChuanYuanKuDongTaiDO>
        implements ChuanYuanKuDongTaiService {

    @Resource
    private ChuanYuanKuDongTaiMapper chuanYuanKuDongTaiMapper;

    @Resource
    private ISystemUserService systemUserService;

    /**
     * 船员动态列表数据
     */
    public IPage<ChuanYuanKuDongTaiMapperVO> findDongTai(ChuanYuanKuDongTaiMapperSearchPO searchPO) {
        Date currentDate = new Date();

        // 查询用户分组
        SysUserDetailVO sysUserDetailVO = systemUserService.selectById(UserThreadLocal.get().getId());
        String groupIds = sysUserDetailVO.getGroupIds();
        if (StringUtils.isNotEmpty(groupIds)) {
            List<Long> collect = Arrays.stream(groupIds.split(",")).toList().stream().map(Long::parseLong).collect(Collectors.toList());
            searchPO.setFenZuIds(collect);
        }

        IPage<ChuanYuanKuDongTaiMapperVO> page = new Page<>(searchPO.getPageNo(), searchPO.getPageSize());
        IPage<ChuanYuanKuDongTaiMapperVO> page1 = chuanYuanKuDongTaiMapper.findList(page,searchPO);
        List<ChuanYuanKuDongTaiMapperVO> records = page1.getRecords();
        //DateUtils.stringToDate
        records.forEach(chuanYuanKuDongTaiMapperVO ->{
            chuanYuanKuDongTaiMapperVO.setCaoZuo("新增跟踪记录");
            chuanYuanKuDongTaiMapperVO.setTiXing("");
            //下次处理日期
            Date chuLiRiQi = DateUtils.stringToDate(chuanYuanKuDongTaiMapperVO.getChuLiRiQi());
            if (chuLiRiQi != null && chuLiRiQi.after(currentDate)) {    //如果下次处理日期 早于 当前日期
                chuanYuanKuDongTaiMapperVO.setCaoZuo("处理");
                chuanYuanKuDongTaiMapperVO.setTiXing(chuanYuanKuDongTaiMapperVO.getChuLiRiQi()+" 需要处理");
            }
        });
        return page1;
    }


    /**
     * 查询分页列表
     */
    @Override
    public Page<ChuanYuanKuDongTaiVO> findChuanYuanKuDongTai(ChuanYuanKuDongTaiSearchPO searchPO) {
        LambdaQueryWrapper<ChuanYuanKuDongTaiDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(searchPO.getZhuangTai()), ChuanYuanKuDongTaiDO::getZhuangTai, searchPO.getZhuangTai());
        lambdaQueryWrapper.eq(Objects.nonNull(searchPO.getChuanyuanId()), ChuanYuanKuDongTaiDO::getChuanYuanId, searchPO.getChuanyuanId());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(searchPO.getKeyword()), ChuanYuanKuDongTaiDO::getNeiRong, searchPO.getKeyword());
        lambdaQueryWrapper.orderByDesc(ChuanYuanKuDongTaiDO::getCreateTime);
        Page<ChuanYuanKuDongTaiDO> page = new Page<>(searchPO.getPageNo(), searchPO.getPageSize());
        Page<ChuanYuanKuDongTaiDO> page1 = this.page(page, lambdaQueryWrapper);
        return tranferPageBo(page1, ChuanYuanKuDongTaiVO.class);
    }


    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveChuanYuanKuDongTai(ChuanYuanKuDongTaiSavePO savePO) {
        savePO.setId(null); //新增不需要id
        ChuanYuanKuDongTaiDO chuanYuanKuDongTaiDO = tranferBo(savePO, ChuanYuanKuDongTaiDO.class);
        if (Objects.nonNull(chuanYuanKuDongTaiDO)) {
            chuanYuanKuDongTaiDO.setJiLuZheId(UserThreadLocal.get().getId());
            chuanYuanKuDongTaiDO.setJiLuZheNiCheng(UserThreadLocal.get().getUsername());
            chuanYuanKuDongTaiDO.setJiLuZheZhangHao(UserThreadLocal.get().getAccount());
            chuanYuanKuDongTaiDO.setSystemId(UserThreadLocal.get().getSystemId());
            chuanYuanKuDongTaiDO.setSystemName(UserThreadLocal.get().getSystemName());
            return this.save(chuanYuanKuDongTaiDO);
        }
        return false;
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateChuanYuanKuDongTai(ChuanYuanKuDongTaiSavePO savePO) {
        if (savePO.getId() == null) {
            throw new RuntimeException("id不能为空");
        }

        ChuanYuanKuDongTaiDO chuanYuanKuDongTaiDO = tranferBo(savePO, ChuanYuanKuDongTaiDO.class);
        if (Objects.nonNull(chuanYuanKuDongTaiDO)) {
            return this.updateById(chuanYuanKuDongTaiDO);
        }
        return false;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delChuanYuanKuDongTai(Long id) {
        return this.removeById(id);
    }


    /**
     * 状态修改。
     * 0:停用、1:启用
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatus(StatusRequest statusRequest) {
        if (ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return this.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        } else {
            return this.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }


    @Override
    public ChuanYuanKuDongTaiVO getChuanYuanKuDongTai(Long id) {
        ChuanYuanKuDongTaiDO chuanYuanKuDongTaiDO = getById(id);
        return tranferBo(chuanYuanKuDongTaiDO, ChuanYuanKuDongTaiVO.class);
    }




}
